package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.presell.PresellConfig;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendPresellMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendPresellService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BackendPresellServiceImpl implements BackendPresellService {

    @Autowired
    private BackendPresellMapper backendPresellMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    @Override
    public String getImg(String account) {
        return backendPresellMapper.findImg(account)+"?"+((Math.random() * 9 + 1) * 100000);
    }

    @Override
    public String upImg(MultipartFile file, String account) throws ErrorDataException {

        if (null == file || file.isEmpty()) {
            System.out.println("文件为空");
            throw new ErrorDataException("请上传新图片");
        }

        String fileName = file.getOriginalFilename(); //文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 后缀名

        String filePath = "//root//code//img//" + account + "-shop//presell//"; // 上传后的路径
        fileName = "presell" + suffixName; // 新文件名


        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ErrorDataException("系统错误");
        } catch (Exception e) {
            System.err.println(e.getMessage());
            throw new ErrorDataException("系统错误");
        }
        String img = "http://lfwdcdn.cloudsmicro.com/img/" + account + "-shop/presell/" + fileName;

        if (null == backendPresellMapper.findImg(account)){
            backendPresellMapper.addImg(img,account);
        }else {
            backendPresellMapper.upImg(img,account);
        }

        return img;
    }

    @Override
    public void upAgreement(String body, String token) {
        backendPresellMapper.upAgreement(body, token);

    }

    @Override
    public String getAgreement(String token) {
        return backendPresellMapper.getAgreement(token);
    }

    @Override
    public void delPresell(String id) {
        backendPresellMapper.delPresell(id);
    }

    /**
     * 新增预售
     * @param presellConfig
     * @throws ErrorDataException
     */
    @Override
    public void createPresell(PresellConfig presellConfig) throws ErrorDataException {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            presellConfig.setCreateTime(sdf.format(new Date()));
            presellConfig.setGid(backendGoodsMapper.getGidByCode(presellConfig.getCode(),presellConfig.getAccount()));

            backendPresellMapper.createPresell(presellConfig);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException(e.getMessage());
        }
        backendGoodsMapper.upGoodsState(presellConfig.getGid(),"N");
    }

    @Override
    public List<PresellConfig> getData(String token) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        int ndate = Integer.parseInt(sdf.format(new Date()));
        List<PresellConfig> data = backendPresellMapper.getData(token);
        for (PresellConfig p : data){
            if (ndate >= Integer.parseInt(p.getDepositTime())){
                p.setState(1);
            }
            if (ndate >= Integer.parseInt(p.getClearingTime())){
                p.setState(2);
            }
            if (ndate >= Integer.parseInt(p.getEndTime())){
                p.setState(3);
            }
            p.setGoods(backendGoodsMapper.getGoodsById(p.getGid()));
        }
        return data;
    }

}
